package com.filldream.fastboot.common.okutil.stock.ths.comment.dto;

import cn.hutool.core.date.DateUtil;
import com.filldream.fastboot.common.okutil.stock.BaseUtil;
import com.filldream.fastboot.common.okutil.stock.cf.comment.dto.CfComment;
import com.filldream.fastboot.common.okutil.stock.cf.comment.dto.CfCommentPostUser;
import com.filldream.fastboot.common.okutil.stock.cf.comment.dto.CfCommentReply;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Data
public class ThsComment {

    private Long id;

    private String content;

    private List<String> imgArr;

    private Long ctime;

    private String createTime;

    private List<ThsCommentReply> comments;

    private String stockName;

    private String simpleCode;

    private String searchCode;

    private ThsCommentReplyUser user;

    public String getPrintContent( ) {
        String result;
        String realContent = content;
        this.createTime = DateUtil.date(ctime*1000).toString("yyyy-MM-dd HH:mm:ss");
        if( content.contains("hx_stock") ){
            int start = content.indexOf("</hx_stock>") + 11;
            realContent = content.substring(start);
            String stockName1 = getSuffix(content, "stockName");
            if(StringUtils.isNotBlank(stockName1) && StringUtils.isBlank(stockName)){
                this.stockName = stockName1;
            }
        }
        if(StringUtils.isBlank(stockName)){
            this.stockName = "THS";
        }
        result =  this.createTime +  "  " + this.stockName + "-"
                + BaseUtil.delHtmlTag(realContent.trim())
                + "  [" + user.getNickname() + "-" + user.getStockAge() +  "]";
        return "\r\n" + result + "\r";
    }

    /**
     * 获取股票信息
     * @param content   内容
     * @param key   key
     * @return
     */
    public static String getSuffix(String content,String key) {
        // 定义匹配 stockName 的正则表达式
        Pattern pattern = Pattern.compile(key + ":(.*?),");
        Matcher matcher = pattern.matcher(content);
        // 如果找到匹配项，则提取股票名称
        if (matcher.find()) {
            String value = matcher.group(1);
            return value;
        }
        return "";
    }

    public  CfComment toCfComment(){
        CfComment result = new CfComment();
        result.setChannelType(2);
        result.setSearchCode( searchCode );
        result.setPost_id( id );
        result.setTheme( "" );
        result.setPost_title("");
        result.setPost_pic_url( imgArr );
        result.setPost_publish_time( DateUtil.date(ctime*1000).toString("yyyy-MM-dd HH:mm:ss") );
        result.setPost_content(  content );
        result.setStockName(getSuffix(content, "stockName"));
        //评论者
        CfCommentPostUser cfCommentPostUser = new CfCommentPostUser();
        cfCommentPostUser.setUser_nickname( user.getNickname() );
        cfCommentPostUser.setUser_age( user.getStockAge() );
        result.setPost_user( cfCommentPostUser);
        //回复者
        List<CfCommentReply> replyList = new ArrayList<>();
        for (ThsCommentReply thsCommentReply : comments) {
            CfCommentReply cfCommentReply = new CfCommentReply();
            cfCommentReply.setReply_id( thsCommentReply.getCid() );
            cfCommentReply.setReply_text( thsCommentReply.getContent() );
            cfCommentReply.setReply_time( null );
            //回复者信息
            CfCommentPostUser replyUser = new CfCommentPostUser();
            replyUser.setUser_nickname( thsCommentReply.getFromUser().getNickname() );
            replyUser.setUser_age( thsCommentReply.getFromUser().getStockAge() );
            cfCommentReply.setReply_user(replyUser);
            replyList.add(cfCommentReply);
        }
        result.setReply_list(replyList);
        //重置内容
        String realContent = content;
        if( content.contains("hx_stock") ){
            int start = content.indexOf("</hx_stock>") + 11;
            realContent = content.substring(start);
        }
        realContent = BaseUtil.delHtmlTag(realContent.trim());
        result.setPost_content(realContent);
        //重置股票名称
        if( StringUtils.isBlank(result.getStockName()) ){
            result.setStockName( searchCode );
        }
        return result;
    }

}
